package com.example.taobaou.presenter.impl;

import com.example.taobaou.model.Api;
import com.example.taobaou.model.domain.OnSellContent;
import com.example.taobaou.presenter.IOnSellPagePresenter;
import com.example.taobaou.utils.LogUtils;
import com.example.taobaou.utils.RetrofitManager;
import com.example.taobaou.utils.UrlUtils;
import com.example.taobaou.view.IOnSellPageCallback;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class OnSellPagePresenterImpl implements IOnSellPagePresenter {

    public static final int DEFUALT_PAGE = 1;
    private int mCurrentPage = DEFUALT_PAGE;
    private IOnSellPageCallback mOnSellPageCallback = null;
    private final Api api;


    public OnSellPagePresenterImpl() {
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        api = retrofit.create(Api.class);
    }

    @Override
    public void getOnSellContent() {
        if (isLoading) {
            return;
        }
        isLoading = true;
        //通知ui状态为加载中
        if (mOnSellPageCallback != null) {
            mOnSellPageCallback.onLoading();
        }

        //获取特惠内容
        String realUrl = UrlUtils.getOnSellPageUrl(mCurrentPage);
        Call<OnSellContent> task = api.getOnSellPageContent(realUrl);
        task.enqueue(new Callback<OnSellContent>() {
            @Override
            public void onResponse(Call<OnSellContent> call, Response<OnSellContent> response) {
                isLoading = false;
                int code = response.code();
                LogUtils.d(this, "OnSell code------------------------->" + code);
                if (code == HttpURLConnection.HTTP_OK) {
                    OnSellContent result = response.body();
                    onSuccess(result);
                } else {
                    LogUtils.d(OnSellPagePresenterImpl.this, "请求失败11---------------------->");
                    onError();
                }
            }

            @Override
            public void onFailure(Call<OnSellContent> call, Throwable t) {
                LogUtils.d(OnSellPagePresenterImpl.this, "请求失败22---------------------->" + t);
                onError();
            }
        });


    }

    private void onSuccess(OnSellContent result) {
        if (mOnSellPageCallback != null) {
            try {
                if (isEmpty(result)) {
                    onEmpty();
                } else {
                    mOnSellPageCallback.onContentloadeSuccess(result);
                }
            } catch (Exception e) {
                e.printStackTrace();
                onEmpty();
            }

        }
    }

    private boolean isEmpty(OnSellContent content) {
        int size = content.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data().size();
        return size == 0;
    }

    private void onError() {
        isLoading = false;
        if (mOnSellPageCallback != null) {
            mOnSellPageCallback.onNatworkError();
        }
    }

    private void onEmpty() {
        if (mOnSellPageCallback != null) {
            mOnSellPageCallback.onEmpty();
        }
    }

    @Override
    public void reLoad() {
        //重新加载
        this.getOnSellContent();
    }

    //当前加载状态
    private boolean isLoading = false;

    @Override
    public void loaderMore() {
        if (isLoading) {
            return;
        }
        isLoading = true;
        //加载更多
        mCurrentPage++;
        //获取更多特惠内容
        String realUrl = UrlUtils.getOnSellPageUrl(mCurrentPage);
        Call<OnSellContent> task = api.getOnSellPageContent(realUrl);
        task.enqueue(new Callback<OnSellContent>() {
            @Override
            public void onResponse(Call<OnSellContent> call, Response<OnSellContent> response) {
                isLoading = false;
                int code = response.code();
                LogUtils.d(this, "OnSell code------------------------->" + code);
                if (code == HttpURLConnection.HTTP_OK) {
                    OnSellContent result = response.body();
                    onLoaderMore(result);
                } else {
                    onLoaderMoreError();
                }
            }

            @Override
            public void onFailure(Call<OnSellContent> call, Throwable t) {
                onLoaderMoreError();
            }
        });

    }

    private void onLoaderMore(OnSellContent result) {

        if (mOnSellPageCallback != null) {
            if (isEmpty(result)) {
                mCurrentPage--;
                mOnSellPageCallback.onMoreLoadedEmpty();
            } else {
                mOnSellPageCallback.onMoreLoaded(result);
            }
        }
    }

    private void onLoaderMoreError() {
        isLoading = false;
        mCurrentPage--;
        mOnSellPageCallback.onMoreLoadedError();
    }


    @Override
    public void registerViewCallback(IOnSellPageCallback callback) {
        this.mOnSellPageCallback = callback;
    }

    @Override
    public void unregisiterViewCallback(IOnSellPageCallback callback) {
        mOnSellPageCallback = null;
    }
}

